<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div style="margin-right: 20px;margin-top: 10px">
    <form class="layui-form" action="javascript:;" id="selectStockForm">
        <#if !info?has_content>
        <div class="layui-form-item">
            <label class="layui-form-label">选择类型</label>
            <div class="layui-input-inline">
                <select name="gameKind" id="gameKind" lay-filter="gameKind">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择平台</label>
            <div class="layui-input-inline">
                <select name="gamePlatform" id="gamePlatform" lay-filter="gamePlatform">

                </select>
            </div>
        </div>
        </#if>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">游戏名称</label>
                <div class="layui-input-inline">
                    <input name="gameId" hidden="hidden" <#if info?has_content>value="${info.gameId}"</#if>>
                    <input type="text" name="gameName"  autocomplete="off" class="layui-input" <#if info?has_content>value="${info.gameName}"</#if>>
                </div>
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">洗码比例</label>
            <div class="layui-input-block">
                <input type="text" name="gameXima" lay-verify="required|number" autocomplete="off" placeholder="请输入洗码比例"
                       class="layui-input" <#if info?has_content>value="${info.gameXima}"</#if>>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>
<script src="/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['table','element','form','layer'], function(){
        var $ = layui.jquery
            ,table = layui.table
            ,layer = layui.layer
            ,form = layui.form
            ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块


        form.render();

         var index = parent.layer.getFrameIndex(window.name);

        //遍历游戏类型菜单
        var gameKindId = '<option value=""></option>';
        $.ajax({
            type: "POST",
            url: "/game/getGameKind",
            traditional: true,
            dataType: "json",
            async: false,
            contentType: "application/json",
            success: function (list) {
                for (var i = 0; i < list.length; i++) {
                    var id = list[i].id;
                    gameKindId += '<option value="'+ id + '">' + list[i].gameKind + '</option>';
                }
            },
            error: function () {
                console.log("请求类型失败！");
            }
        });
        $("#gameKind").html(gameKindId);
        form.render();


        // $("#gameKind").change(function(){
        form.on('select(gameKind)', function(data){
            var kindId=$("#gameKind").val();
            //遍历游戏平台菜单
            var gamePlatformId = '<option value=""></option>';
            $.ajax({
                type: "POST",
                url: "/game/getPlatform/"+kindId,
                traditional: true,
                dataType: "json",
                async: false,
                contentType: "application/json",
                success: function (list) {
                    for (var i = 0; i < list.length; i++) {
                        var id = list[i].id;
                        gamePlatformId += '<option value="'+ id + '">' + list[i].gamePlatform + '</option>';
                    }
                },
                error: function () {
                    console.log("请求平台失败！");
                }
            });
            $("#gamePlatform").html(gamePlatformId);
            form.render();
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            // var data.field.test = $("#gamePlatForm").val();
            var json = JSON.stringify(data.field);
            console.log("json="+json);
            $.ajax({
                type: "POST",
                url: "/game/add",
                data: json,
                // dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data == 'success'){
                        // layer.msg('添加成功！', {
                        //     icon: 1,
                        //     time: 300 //如果不配置，默认是3秒
                        // }, function () {
                        //     var index = parent.layer.getFrameIndex(window.name);
                        //     parent.layer.close(index);
                        // });
                        //得到弹出层的index关闭
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);

                    }else{
                        layer.msg('新增失败');
                    }
                },
                error: function (data) {
                    try {
                        layer.msg(JSON.parse(data.responseText).message, {icon: 1, time: 1000});
                    }catch (e) {
                        layer.msg("新增失败");
                    }
                }
            });
        });



    });
</script>

</body>
</html>